﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Web;
using System.Net;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;

namespace iPublic
{
    /// <summary>
    /// 公共函数库，周承昊
    /// 修改记录：
    ///   20171212，zch，创建基本功能
    /// </summary>
    public partial class iPublicX
    {

        #region //getGPS地址，根据网络IP地址获取GPS位置和中文县区街道地址
        /// <summary>
        /// 根据网络IP地址获取GPS位置和中文县区街道地址，参考百度的：
        ///   http://lbsyun.baidu.com/apiconsole/key
        ///   https://www.cnblogs.com/zhaotianff/p/5988177.html
        ///   应用AK：hhCEw1lxrR6yR8NDyKLBfXgcQysdUwPP
        /// </summary>
        /// <param name="sBaiduKey"></param>
        /// <returns></returns>
        public static iDefine.ChuLiJieGuo getGPS地址(string sBaiduKey = "", string s坐标格式 = "bd09ll")
        {
            iDefine.ChuLiJieGuo result = new iPublic.iDefine.ChuLiJieGuo(), jg = null;
            string s = "", sUrl = "https://api.map.baidu.com/location/ip?ak=hhCEw1lxrR6yR8NDyKLBfXgcQysdUwPP&coor=bd09ll";
            try
            {
                if (string.IsNullOrEmpty(sBaiduKey)) sBaiduKey = iDefine.SysConfig.sBaiduKey;   //3Mx1Wn88ol5iDG6X99a2uUWaxFfdsspV
                if (string.IsNullOrEmpty(s坐标格式)) s坐标格式 = "bd09ll";
                sUrl = "https://api.map.baidu.com/location/ip?ak=" + sBaiduKey + "&coor=" + s坐标格式;
                //读取
                if ((jg = httpGet(sUrl, ref s)).error != 0)
                    throw new Exception("获取GPS地址通信错误：" + jg.error.ToString() + "," + jg.message);

                //分析返回值，可以从这里看json解析：https://www.bejson.com
                iDefine.jsonBaiduGPS地址 baiduGps = JsonConvert.DeserializeObject<iDefine.jsonBaiduGPS地址>(s);
                if (baiduGps == null) throw new Exception("解析百度返回值出错：" + s);
                baiduGps.decode();

                //完成
                result.error = 0;
                result.data = baiduGps;
                result.message = "ok";
            }
            catch (Exception x)
            {
                if (result.error <= 0) result.error = 100;
                result.message = x.Message;
            }
            return result;
        }
        #endregion
        

        #region //showMessage，显示提示信息
        public static DialogResult showMessage(string sInfo, string sCaption="", int nTime = 0)
        {
            DialogResult result = DialogResult.OK;
            SysTools.FrmHint frm = new SysTools.FrmHint();
            if (!string.IsNullOrEmpty(sInfo)) frm.txt_info.Text = sInfo;
            if (!string.IsNullOrEmpty(sCaption)) frm.lbl_caption.Text = sCaption;
            if (nTime > 0) frm.lbl_time.Text = nTime.ToString();
            result = frm.ShowDialog();
            frm.Dispose();
            return result;
        }
        #endregion


        #region  //Http请求，httpGet
        public static iDefine.ChuLiJieGuo httpGet(string url, ref string responseText)
        {
            iDefine.ChuLiJieGuo result = new iPublic.iDefine.ChuLiJieGuo();
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader sr = null;
            try
            {
                // 创建一个HTTP请求
                request = (System.Net.HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 30 * 1000;    //30秒
                request.Method = "GET";
                response = (System.Net.HttpWebResponse)request.GetResponse();
                //请求状态正确
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new Exception("请求出错");
                }
                sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);   //Encoding.GetEncoding("gb2312")
                responseText = sr.ReadToEnd();
                //
                result.error = 0;
            }
            catch (Exception ex)
            {
                responseText = ex.Message;
                result.error = 100;
                result.message = ex.Message;
            }
            finally
            {
                if (sr != null) sr.Close();
            }
            return result;
        }
        #endregion


        #region //C#没有自己的输入框，自己做的一个inputBox
        //public static string inputBox(string Caption, string Hint, string Default)
        //{
        //    //窗体
        //    Form InputForm = new Form();
        //    InputForm.MinimizeBox = false;
        //    InputForm.MaximizeBox = false;
        //    InputForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        //    //InputForm.StartPosition = FormStartPosition.CenterScreen;
        //    InputForm.Width = 220;
        //    InputForm.Height = 150;
        //    //生成一个菜单，windowsCE没有菜单底下的输入法框不出来
        //    MainMenu mnu = new MainMenu();
        //    MenuItem mnuReturn = new MenuItem();
        //    mnu.MenuItems.Add(mnuReturn);
        //    InputForm.Menu = mnu;
        //    //标题
        //    InputForm.Text = Caption;
        //    //提示内容
        //    Label lbl = new Label();
        //    lbl.Parent = InputForm;
        //    lbl.Text = Hint;
        //    lbl.Left = 10;
        //    lbl.Width = InputForm.Width - lbl.Left * 2;
        //    //lbl.AutoSize = true;
        //    lbl.Top = 20;
        //    lbl.Height = lbl.Height * 2 + 10;
        //    //输入框
        //    TextBox tb = new TextBox();
        //    tb.Left = 30;
        //    tb.Top = lbl.Top + lbl.Height + 20;
        //    tb.Width = 160;
        //    tb.Parent = InputForm;
        //    tb.Text = Default;
        //    tb.SelectAll();
        //    //确定按钮
        //    Button btnok = new Button();
        //    btnok.Left = 30;
        //    btnok.Top = tb.Top + tb.Height + 20;
        //    btnok.Parent = InputForm;
        //    btnok.Text = "确定";
        //    //InputForm.AcceptButton = btnok;//回车响应  
        //    btnok.DialogResult = DialogResult.OK;
        //    //取消按钮
        //    Button btncancal = new Button();
        //    btncancal.Left = 120;
        //    btncancal.Top = btnok.Top;
        //    btncancal.Parent = InputForm;
        //    btncancal.Text = "取消";
        //    btncancal.DialogResult = DialogResult.Cancel;
        //    //显示窗体
        //    try
        //    {
        //        if (InputForm.ShowDialog() == DialogResult.OK)
        //        {
        //            return tb.Text;
        //        }
        //        else
        //        {
        //            return null;
        //        }
        //    }
        //    finally
        //    {
        //        InputForm.Dispose();
        //    }
        //}
        #endregion

    }

}
